liwe.form.instance.prototype.calendar = function ( vars )
{
	var attrs = {};
	attrs [ 'time' ] = vars.get ( "time", 0 );

	vars [ 'os3_class_value' ] = '';

	this._start_field ( vars );

	var calname = "cal-" + this.name + '-' + vars [ 'name' ];
	var fname = this.name + '@' + vars [ 'name' ];


	/*
	var cal = new WWL.calendar ( vars [ 'name' ], attrs );
	this.hidden ( { name: vars [ 'name' ], id: "cal-" + vars [ 'name' ], value: 0 } );
	*/

	var cal = new WWL.calendar ( calname, attrs );
	this.hidden ( { name: vars [ 'name' ], value: 0 } );

	this.html += cal.to_string ();

	//this._widgets [ calname ] = cal;
	this._widgets [ vars [ 'name' ] ] = cal;

	this._newline ( vars );

	var self = this;

	cal.set_event ( "date_changed", function ( year, month, day, calendar )
	{
		console.debug ( "DATE CHANGED: year: %s - month: %s - day: %s", year, month, day );

		cal.set_day ( day );
		cal.set_month ( month + 1 );
		cal.set_year ( year );

		if ( vars [ 'date_changed' ] ) vars [ 'date_changed' ] ( year, month, day );

		// self.set_value ( "cal-" + vars [ 'name' ], cal.get_value () );
		//self.set_value ( vars [ 'name' ], cal.get_value () );
	} );

	cal.get_value = function ()
	{
		var d = cal._calendar.cur_date;

		if ( cal._support_time )
		{
			var hh = cal.form.get_value ( "hour" );
			var mm = cal.form.get_value ( "min" );
			return String.format ( "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:00", d.getFullYear (), d.getMonth () + 1, d.getDate (), hh, mm );
		}

		return String.format ( "%4.4d-%2.2d-%2.2d", d.getFullYear (), d.getMonth () + 1, d.getDate () );
	};

	cal.set_value = function ( val, quiet )
	{
		if ( ! val ) val = '2001-01-01';

		var re = /([1-9][0-9]{3}).([0-9]{1,2})[^0-9]?([0-9]{1,2})[^0-9]*([0-9]*):*([0-9]*).*/
		var g = val.match ( re );

		cal.set_date ( g [ 1 ], g [ 2 ], g [ 3 ], g [ 4 ], g [ 5 ], quiet );

		if ( $( fname ) )
			$( fname ).value = val;
	};

	cal.render = function ()
	{
		var d = cal._calendar.cur_date;

		cal.set_day ( d.getDate () );
		cal.set_month ( d.getMonth () +1 );
		cal.set_year ( d.getFullYear () );

		cal.set_time ();
	};

	cal._refresh = function ()
	{
		self.set_value ( vars [ 'name' ], cal.get_value () );
	};


	if ( vars [ 'value' ] ) 
	{
		cal.set_value ( vars [ 'value' ], true );
	}

	if ( vars [ 'show_navi_years' ] ) cal.set_attr ( 'show_navi_years', true );

	return cal;
};
